package com.example.parallelandroid;

import static com.mob.MobSDK.getContext;

import android.content.Context;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.parallelandroid.entity.Setting;

import java.util.ArrayList;
import java.util.List;

public class MySettingAdapter extends BaseAdapter {
    //加载布局文件使用
    private Context mContext;
    //展示的数据源
    private List<String> Strings = new ArrayList<>();
    //传递布局文件id
    private int layoutResId;

    public MySettingAdapter(Context mContext, List<String> settings, int layoutResId) {
        this.mContext = mContext;
        this.Strings = settings;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (Strings!=null){
            return Strings.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (Strings!=null){
            return Strings.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            //加载布局文件，生成view对象，给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item控件对象
            viewHolder = new ViewHolder();
            //viewHolder.ivImg = convertView.findViewById(R.id.iv_image);
            viewHolder.tvText = convertView.findViewById(R.id.tv_tv_text);
            Typeface handwriting = Typeface.createFromAsset(getContext().getAssets(),
                    "狮尾黑宋体.ttf");
            viewHolder.tvText.setTypeface(handwriting);
            //红色渐变色
              LinearGradient redLinearGradient = new LinearGradient(0, 0, 0, viewHolder.tvText.getPaint().getTextSize(), Color.GRAY, Color.BLACK, Shader.TileMode.CLAMP);
            viewHolder.tvText.getPaint().setShader(redLinearGradient);
            //将ViewHolder与convertView关联起来
            convertView.setTag(viewHolder);
        }else{//重复利用convertView对象
            //获取到跟当前convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //viewHolder.ivImg.setImageResource(settings.get(i).getImg());
        viewHolder.tvText.setText(Strings.get(i));
        return convertView;
    }

    //定义静态内部类
    private static class ViewHolder{
        TextView tvText;
    }
}
